package org.liuyunfeng.web;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/**
 * @author: TanJingyu
 * @create: 2022-06-21 14:51
 **/
@RestController
@RequestMapping("/semaphore")
public class SemaphoreController {
    private final Semaphore semaphore = new Semaphore(3);

    @GetMapping
    public Object a() throws InterruptedException {
        semaphore.acquire(3);
        System.out.println(Thread.currentThread().getName() + "拿到，睡觉");
        TimeUnit.SECONDS.sleep(6);
        System.out.println("睡醒");
        semaphore.release(3);
        return semaphore.availablePermits();
    }
}

    